package com.example.smartcity_40.me

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.example.smartcity_40.R
import com.example.smartcity_40.databinding.ActivityUserInfoBinding
import com.example.smartcity_40.tool.Tool
import com.example.smartcity_40.tool.tool
import com.example.smartcity_40.tool.viewBinding
import org.json.JSONObject

class UserInfoActivity : AppCompatActivity() {
    private val vb by viewBinding(ActivityUserInfoBinding::inflate)
    val TAG = "UserInfoActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vb.root)
        var sex = "0"
        tool.apply {
            send("/prod-api/api/common/user/getInfo",Tool.GET,null,true,MeUserInfoBean::class.java) {
                val data = it.user
                vb.userInfoName.setText(it.user.userName)
                vb.userInfoEmail.setText(it.user.email)
                vb.userInfoPhone.setText(it.user.phonenumber)
                when(it.user.sex) {
                    "1" ->  vb.userInfoWoman.isChecked = true
                    else -> vb.userInfoMan.isChecked = true
                }
                vb.userInfoMan.setOnClickListener {
                    sex = "0"
                }
                vb.userInfoWoman.setOnClickListener {
                    sex = "1"
                }
                vb.userInfoBtn.setOnClickListener {
                    val data = """
                        {
                        "email": "${vb.userInfoEmail.text}",
                        "idCard": "${data.idCard}",
                        "nickName": "${vb.userInfoName.text}",
                        "phonenumber": "${vb.userInfoPhone.text}",
                        "sex": "$sex"
                        }
                    """.trimIndent()
                    tool.apply {
                        send("/prod-api/api/common/user",Tool.PUT,data,true) {
                            Log.e(TAG, "onCreate: ${JSONObject(it).getString("msg")}")
                            if (it.contains("操作成功")) {
                                Toast.makeText(context,"修改成功",Toast.LENGTH_SHORT).show()
                                finish()
                            } else {
                                Toast.makeText(context,JSONObject(it).getString("msg"),Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                }
            }
        }
    }
}